package com.myhkvideo.yulongbpcdemo.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.myhkvideo.yulongbpcdemo.entity.RawOreCBLDayInfoData;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
//全矿日出剥量
public class RawOreCBLDayInfoService {
    @Autowired
    private LoginService loginService;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private ObjectMapper objectMapper;

    private static final String LOGIN_URL = "http://ylty.westmining.com:60016/api/BoardProductionCommand/RawOreCBLDayInfo";
    //获取token存入reqyestEntity


    @SneakyThrows
    public ResponseEntity<String> getRawOreCBLDayInfo() {
        String token = loginService.getNewToken();
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Authorization", "Bearer " + token);

        HttpEntity<String> requestEntity = new HttpEntity<>(headers);
        ResponseEntity<String> loginResponse = restTemplate.exchange(LOGIN_URL, HttpMethod.POST, requestEntity, String.class);
        return loginResponse;
    }
    /**
     * 获取并转换数据为RawOreCBLDayInfoData列表
     * @return RawOreCBLDayInfoData列表
     * @throws IOException
     * @throws ParseException
     */
    public List<RawOreCBLDayInfoData> getAndConvertRawOreCBLDayInfo() throws IOException, ParseException {
        ResponseEntity<String> response = getRawOreCBLDayInfo();
        Map<String, Object> loginBody = objectMapper.readValue(response.getBody(), Map.class);
        List<Map<String, Object>> dataList = (List<Map<String, Object>>) loginBody.get("Data");
        return convertToRawOreCBLDayInfoDataList(dataList);
    }

    @SneakyThrows
    private List<RawOreCBLDayInfoData> convertToRawOreCBLDayInfoDataList(List<Map<String, Object>> dataList) throws ParseException {
        List<RawOreCBLDayInfoData> RawOreCBLDayInfoDataList = new ArrayList<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        for (Map<String, Object> dataMap : dataList) {
            RawOreCBLDayInfoData RawOreCBLDayInfoData = new RawOreCBLDayInfoData();
            RawOreCBLDayInfoData.setCreateTime(dateFormat.parse((String) dataMap.get("createtime")));
            RawOreCBLDayInfoData.setD_ycjckl(new BigDecimal((Double) dataMap.get("d_ycjckl")));
            RawOreCBLDayInfoData.setD_ecjckl(new BigDecimal((Double) dataMap.get("d_ecjckl")));
            RawOreCBLDayInfoData.setD_ylcckl(new BigDecimal((Double) dataMap.get("d_ylcckl")));
            RawOreCBLDayInfoData.setD_totalckl(new BigDecimal((Double) dataMap.get("d_totalckl")));
            RawOreCBLDayInfoData.setD_totalbyl(new BigDecimal((Double) dataMap.get("d_totalbyl")));
            RawOreCBLDayInfoDataList.add(RawOreCBLDayInfoData);
        }
        return RawOreCBLDayInfoDataList;
    }


}